# -*- coding: utf-8 -*-
"""
Created on Mon Nov 16 21:47:49 2020

@author: jyh
"""
import numpy as np
import re
from sklearn.preprocessing import OneHotEncoder

def labelled_data_read_file(f_dir,mode=1,num=5000):
    #f_dir为文件路径，含文件名和文件格式，如 C:\Users\jyh\Desktop\test.dat
    #mode用来指示读入的文件是16进制还是2进制，mode=1代码16进制，mode=0代表2进制
    #num代码读取的数据量

    with open(f_dir,'r') as f1:
        d_orgin = f1.readlines()
    data = []
    if mode == 1:
        for it in d_orgin:
            temp1 = re.findall(".{2}", it)
            temp2 = list(map(lambda x:int(x,16),temp1))
            data.append(temp2)
            pass
    elif mode == 0:
        for it in d_orgin:
            temp1 = re.findall(".{8}", it)
            temp2 = list(map(lambda x:int(x,2),temp1))
            data.append(temp2)
            pass
    data = data[0:num]
    
    l_set = [] #存取data中每条样本的长度
    for it in data:
        l_set.append(len(it))
        pass
    max_len = max(l_set)
    
    data_pad = [] #填充后的数据
    for it in data:
        if len(it) < max_len:
            data_pad.append(it+[0]*(max_len-len(it)))
        else:
            data_pad.append(it)
    data_pad = np.asfarray(data_pad)
        
    file_out = r"out_test_data_"
    label = []
    sel_data = data
    return data_pad,file_out,sel_data,label

if __name__=="__main__":
    f = r"C:\Users\jyh\Desktop\1233.dat"
    d,f_out = labelled_data_read_file(f,1,num=5)[0:2] 